<?php

namespace App\Supports\BankCard\Traits;

use Carbon\Carbon;
use Illuminate\Support\Str;
use Zhuzhichao\BankCardInfo\BankCard as BankCardInterface;
use Illuminate\Validation\ValidationException;
use Rainsens\Dinglog\Facades\Dinglog;


trait HasUserBankCard
{
	
	public function addBnakCard()
	{
		$user = $this;
		$method = request()->method;

		$count = $user->bankCards->where("method",request()->method)->count();
		if($count >= 10){

			fail("最多添加十个银行卡或支付宝!");
		}

		if($method == 'bank'){

			$bankCard = str_replace(" ","",request()->bank_card);
	 		$result = file_get_contents("https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo=".$bankCard."&cardBinCheck=true");
	        $result = json_decode($result);
	       	$bankList = BankCardInterface::getBankList();

	       	if($result->validated == false){
				fail('银行卡号错误！');

	       	}
	       	if(!isset($bankList[$result->bank])){

				fail('该银行暂不支持，请绑定其他银行卡');
	       	}
			$card_info = BankCardInterface::info($bankCard);
			if(!$card_info['validated']){
				throw ValidationException::withMessages(['err' => '卡号不正确']);
			}

			/*$info = $user->bankCards->where('method',request()->method)->where('cert_number',$bankCard)->first();
			if($info){

				fail('当前银行卡已录入！');
			}*/

		}elseif($method =="alipay"){
			$bankCard = '';
			$card_info = ['bank'=> '','bankName' => ''];
			$info = $user->bankCards->where('method',request()->method)->where('alipay_number',request()->alipay_number)->first();
			if($info){

				fail('当前支付宝已录入！');
			}
		}else{
			$bankCard = '';
			$card_info = ['bank'=> '','bankName' => ''];
			

		}

		
		$isDefault = request()->is_default;
		if (request()->is_default) {
			$user->bankCards()->where("method",request()->method)->update(['is_default' => false]);
		}
		
		if($count == 0){

			$isDefault  = 1;
		}
		
		$bank_card = $user->update([
			'bank_abbr' => $card_info['bank'],
			'bank_name' => $card_info['bankName'],
			'cert_number' => $bankCard ?? '',
			'real_name' => request()->real_name ?? '',
			'method' => request()->method,
			'alipay_real_name' => request()->alipay_real_name  ?? '',
			'alipay_number' => request()->alipay_number ?? '',
			'is_default' => $isDefault,
			'wx_image' => request()->wx_image,
		]);

		return $bank_card;
	}
	




	public function updateBnakCard()
	{
		$user = $this;
		$method = request('method');

		$alipayNumber = "";
		$alipayRealName = "";
		if($method == 'bank'){
	 		$result = file_get_contents("https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo=".request()->bank_card."&cardBinCheck=true");
	        $result = json_decode($result);
	       	$bankList = BankCardInterface::getBankList();

	       	if($result->validated == false){
				fail('银行卡号错误！');

	       	}
	       	if(!isset($bankList[$result->bank])){

				fail('该银行暂不支持，请绑定其他银行卡');
	       	}
			$card_info = BankCardInterface::info(request()->bank_card);
			if(!$card_info['validated']){
				throw ValidationException::withMessages(['err' => '卡号不正确']);
			}

			

		}elseif($method == 'alipay'){

			$card_info = ['bank'=> '','bankName' => ''];

			$alipayNumber = request()->alipay_number ? request()->alipay_number : request()->bank_card;
			$alipayRealName = request()->alipay_real_name ? request()->alipay_real_name : request()->real_name;
			$hasSameCard = $user->bankCards()->where("method",request()->method)->where("alipay_number",request()->alipay_number)->where("id","!=",$card->id)->exists();
			if($hasSameCard){

				fail('当前支付宝已录入！');
			}

		}else{
			$card_info = ['bank'=> '','bankName' => ''];

		}

		
		
		$bank_card = $user->update([
			'bank_abbr' => $card_info['bank'],
			'bank_name' => $card_info['bankName'],
			'cert_number' => request()->bank_card ?? '',
			'real_name' => request()->real_name ?? '',
			'alipay_real_name' => $alipayRealName  ?? '',
			'alipay_number' => $alipayNumber ?? ''
		]);

		
		

		return $bank_card;
	}


	public function defaultBankCard()
	{

		return  $this->bankCards()->where("method",'bank')->where("is_default",true)->first();

	}


	public function defaultAlipay()
	{

		return  $this->bankCards()->where("method",'alipay')->where("is_default",true)->first();

	}

	public function defaultWxpay()
	{

		return  $this->bankCards()->where("method",'wxpay')->where("is_default",true)->first();

	}
}
